<template>
  <div class="language-page">
    <!-- 顶部导航 -->
    <ResponsiveNav />

    <!-- 主要内容区域 -->
    <main class="main-content">
      <h1 class="page-title">{{ $t('语言设置') }}</h1>

      <div class="settings-section">
        <div class="section-header">
          <h2>{{ $t('选择您偏好的语言') }}</h2>
        </div>
        <div class="language-list">
          <div v-for="lang in langStore.languageList" :key="lang.id" class="language-item">
            <label class="lang-radio">
              <input
                type="radio"
                :value="lang.langKey"
                v-model="selectedLang"
                name="language"
                class="radio-input"
              >
              <img :src="lang.icon" alt="" class="lang-icon">
              <span class="lang-name">{{ lang.countryName }}</span>
              <span class="lang-code">{{ lang.langKey }}</span>
            </label>
          </div>
        </div>
      </div>

      <div class="settings-footer">
        <p class="help-text">
          {{ $t('我们提供翻译来为了您阅读（包括我们的"使用条件"）是最终版本。') }}
        </p>
        <div class="buttons">
          <button class="btn-cancel" @click="router.back()">{{ $t('取消') }}</button>
          <button class="btn-save" :disabled="loading" @click="submit">{{ loading ? $t('保存中...') : $t('保存更改') }}</button>
        </div>
      </div>
    </main>

    <!-- 页脚 -->
    <Footer />
  </div>
</template>

<script setup>
import {  ref, onMounted } from 'vue'
import ResponsiveNav from '@/components/ResponsiveNav.vue'
import Footer from '@/components/Footer.vue'
import {useRouter} from "vue-router";
import useLangStore from '@/store/modules/lang'
import {ElMessage} from "element-plus";
import {useI18n} from "vue-i18n";

const router = useRouter()
const langStore = useLangStore()
const { t } = useI18n();

const selectedLang = ref(langStore.language)
const loading = ref(false)

const submit = () => {
  loading.value = true
  langStore.setLanguage(selectedLang.value).then(() => {
    ElMessage({
      message: t('设置成功'),
      type: 'success',
      duration: 2000
    })
    setTimeout(() => {
      window.location.reload()
    }, 500)
  }).finally(() => {
    loading.value = false
  })
}

onMounted(() => {

})
</script>

<style lang="scss" scoped>
.language-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.main-content {
  flex: 1;
  padding: 16px;
  max-width: 600px;
  margin: 0 auto;
  width: 100%;
}

.page-title {
  font-size: 24px;
  font-weight: 500;
  margin-bottom: 24px;
  color: #111;
}

.settings-section {
  background: #fff;
  border-radius: 8px;
  border: 1px solid #ddd;
  margin-bottom: 24px;
}

.section-header {
  padding: 16px;
  border-bottom: 1px solid #ddd;

  h2 {
    font-size: 18px;
    color: #111;
    font-weight: 500;
  }
}

.language-list {
  padding: 8px 0;
}

.language-item {
  .lang-radio {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    width: 100%;
    cursor: pointer;

    &:hover {
      background: #f9f9f9;
    }

    .radio-input {
      margin-right: 12px;
    }

    .lang-icon{
      width: 24px;
      height: 24px;
      border-radius: 50%;
    }

    .lang-name {
      flex: 1;
      font-size: 14px;
      color: #111;
    }

    .lang-code {
      color: #666;
      font-size: 13px;
      margin: 0 12px;
    }
  }
}

.settings-footer {
  .help-text {
    font-size: 13px;
    color: #666;
    margin-bottom: 24px;

    a {
      color: #0066c0;
      text-decoration: none;

      &:hover {
        color: #c45500;
        text-decoration: underline;
      }
    }
  }

  .buttons {
    display: flex;
    gap: 12px;
    justify-content: flex-end;

    button {
      padding: 8px 16px;
      border-radius: 3px;
      font-size: 13px;
      cursor: pointer;
    }

    .btn-cancel {
      border: 1px solid #ddd;
      background: #fff;

      &:hover {
        background: #f9f9f9;
      }
    }

    .btn-save {
      border: 1px solid #a88734;
      background: linear-gradient(#f7dfa5, #f0c14b);
      font-weight: bold;

      &:hover {
        background: linear-gradient(#f5d78e, #eeb933);
      }
    }
  }
}
</style>
